package edu.scut.autoProcessor.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;

import edu.scut.autoProcessor.po.InformationPo;

public class InformationDao extends AbstractDao{

	private static final Log log = LogFactory.getLog(InformationDao.class);
	
	private static final int PAGE = 15;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<InformationPo> queryInformation(String hostName, String type, String queryDate, int page){
		SqlSession sqlSession = factory.openSession();
		
		try{
			int offset = (page -1) * PAGE;
			
			Map parameters = new HashMap();
			parameters.put("hostName", hostName);
			parameters.put("type", type);
			parameters.put("queryDate", queryDate);
			parameters.put("page", PAGE);
			parameters.put("offset", offset);
			
			return sqlSession.selectList("alertProcessor.informationMapper.selectInformation", parameters);
		}catch(Exception e){
			log.warn(e.getMessage());
			e.printStackTrace();
			return null;
		}finally{
			sqlSession.close();
		}
	}
	
	public static void insertInformation(InformationPo po){
		SqlSession sqlSession = factory.openSession();
		
		try{
			sqlSession.insert("alertProcessor.informationMapper.insertInfo", po);
		}catch(Exception e){
			log.warn(e.getMessage());
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}
	
	public static void updateInformation(InformationPo po){
		SqlSession sqlSession = factory.openSession();
		
		try{
			sqlSession.update("alertProcessor.informationMapper.updateInfo", po);
		}catch(Exception e){
			log.warn(e.getMessage());
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}
	
	public static void deleteInformation(InformationPo po){
		SqlSession sqlSession = factory.openSession();
		
		try{
			sqlSession.delete("alertProcessor.informationMapper.deleteInfo", po.getId());
		}catch(Exception e){
			log.warn(e.getMessage());
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
	}
}
